home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import sys
- import util
- import BIO
- import Err
- import m2
-
- class RSAError(Exception):
- pass
-
- m2.rsa_init(RSAError)
- no_padding = m2.no_padding
- pkcs1_padding = m2.pkcs1_padding
- sslv23_padding = m2.sslv23_padding
- pkcs1_oaep_padding = m2.pkcs1_oaep_padding
-
- class RSA:
- m2_rsa_free = m2.rsa_free
-
- def __init__(self, rsa, _pyfree = 0):
- self.rsa = rsa
- self._pyfree = _pyfree
-
-
- def __del__(self):
- if getattr(self, '_pyfree', 0):
- self.m2_rsa_free(self.rsa)
-
-
-
- def __len__(self):
- return m2.rsa_size(self.rsa) << 3
-
-
- def __getattr__(self, name):
- if name == 'e':
- return m2.rsa_get_e(self.rsa)
- elif name == 'n':
- return m2.rsa_get_n(self.rsa)
- else:
- raise AttributeError
-
-
- def pub(self):
- return (m2.rsa_get_e(self.rsa), m2.rsa_get_n(self.rsa))
-
-
- def public_encrypt(self, data, padding):
- return m2.rsa_public_encrypt(self.rsa, data, padding)
-
-
- def public_decrypt(self, data, padding):
- return m2.rsa_public_decrypt(self.rsa, data, padding)
-
-
- def private_encrypt(self, data, padding):
- return m2.rsa_private_encrypt(self.rsa, data, padding)
-
-
- def private_decrypt(self, data, padding):
- return m2.rsa_private_decrypt(self.rsa, data, padding)
-
-
- def save_key_bio(self, bio, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
- if cipher is None:
- return m2.rsa_write_key_no_cipher(self.rsa, bio._ptr(), callback)
- else:
- ciph = getattr(m2, cipher, None)
- if ciph is None:
- raise RSAError, 'not such cipher %s' % cipher
- else:
- ciph = ciph()
- return m2.rsa_write_key(self.rsa, bio._ptr(), ciph, callback)
-
-
- def save_key(self, file, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
- bio = BIO.openfile(file, 'wb')
- return self.save_key_bio(bio, cipher, callback)
-
- save_pem = save_key
-
- def as_pem(self, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
- bio = BIO.MemoryBuffer()
- self.save_key_bio(bio, cipher, callback)
- return bio.read()
-
-
- def save_key_der_bio(self, bio):
- return m2.rsa_write_key_der(self.rsa, bio._ptr())
-
-
- def save_key_der(self, file):
- bio = BIO.openfile(file, 'wb')
- return self.save_key_der_bio(bio)
-
-
- def save_pub_key_bio(self, bio):
- return m2.rsa_write_pub_key(self.rsa, bio._ptr())
-
-
- def save_pub_key(self, file):
- bio = BIO.openfile(file, 'wb')
- return m2.rsa_write_pub_key(self.rsa, bio._ptr())
-
-
- def check_key(self):
- return m2.rsa_check_key(self.rsa)
-
-
- def sign(self, digest, algo = 'sha1'):
- digest_type = getattr(m2, 'NID_' + algo, None)
- if digest_type is None:
- raise ValueError, ('unknown algorithm', algo)
-
- return m2.rsa_sign(self.rsa, digest, digest_type)
-
-
- def verify(self, data, signature, algo = 'sha1'):
- digest_type = getattr(m2, 'NID_' + algo, None)
- if digest_type is None:
- raise ValueError, ('unknown algorithm', algo)
-
- return m2.rsa_verify(self.rsa, data, signature, digest_type)
-
-
-
- class RSA_pub(RSA):
-
- def __setattr__(self, name, value):
- if name in ('e', 'n'):
- raise RSAError, 'use factory function new_pub_key() to set (e, n)'
- else:
- self.__dict__[name] = value
-
-
- def private_encrypt(self, *argv):
- raise RSAError, 'RSA_pub object has no private key'
-
-
- def private_decrypt(self, *argv):
- raise RSAError, 'RSA_pub object has no private key'
-
- save_key = RSA.save_pub_key
- save_key_bio = RSA.save_pub_key_bio
-
- def check_key(self):
- return m2.rsa_check_pub_key(self.rsa)
-
-
-
- def rsa_error():
- raise RSAError, m2.err_reason_error_string(m2.err_get_error())
-
-
- def keygen_callback(p, n, out = sys.stdout):
- ch = [
- '.',
- '+',
- '*',
- '\n']
- out.write(ch[p])
- out.flush()
-
-
- def gen_key(bits, e, callback = keygen_callback):
- return RSA(m2.rsa_generate_key(bits, e, callback), 1)
-
-
- def load_key(file, callback = util.passphrase_callback):
- bio = BIO.openfile(file)
- return load_key_bio(bio, callback)
-
-
- def load_key_bio(bio, callback = util.passphrase_callback):
- rsa = m2.rsa_read_key(bio._ptr(), callback)
- if rsa is None:
- rsa_error()
-
- return RSA(rsa, 1)
-
-
- def load_key_string(string, callback = util.passphrase_callback):
- bio = BIO.MemoryBuffer(string)
- return load_key_bio(bio, callback)
-
-
- def load_pub_key(file):
- bio = BIO.openfile(file)
- return load_pub_key_bio(bio)
-
-
- def load_pub_key_bio(bio):
- rsa = m2.rsa_read_pub_key(bio._ptr())
- if rsa is None:
- rsa_error()
-
- return RSA_pub(rsa, 1)
-
-
- def new_pub_key(.0):
- (e, n) = .0
- rsa = m2.rsa_new()
- m2.rsa_set_e(rsa, e)
- m2.rsa_set_n(rsa, n)
- return RSA_pub(rsa, 1)
-
-